package cn.semes.org.service.impl;

import cn.semes.basic.config.BussinessException;
import cn.semes.basic.util.BaiduAuditUtils;
import cn.semes.basic.util.EmailUtil;
import cn.semes.org.domain.Employee;
import cn.semes.org.domain.Shop;
import cn.semes.org.domain.ShopAuditLog;
import cn.semes.org.dto.AuditShopDto;
import cn.semes.org.dto.EmployeeDto;
import cn.semes.org.dto.ShopSettlementDto;
import cn.semes.org.mapper.EmployeeMapper;
import cn.semes.org.mapper.ShopAuditLogMapper;
import cn.semes.org.mapper.ShopMapper;
import cn.semes.org.service.IShopService;
import cn.semes.basic.service.impl.BaseServiceImpl;
import cn.semes.user.domain.Logininfo;
import cn.semes.user.mapper.LogininfoMapper;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author semes
 * @since 2022-11-20
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(ShopSettlementDto dto) { // shopSettlementDto用来接收前端的数据
         // 验证数据
        this.verify(dto);

        // 保存
        this.save(dto);
    }

    @Override
    public void AuditShopReject(AuditShopDto dto) throws Exception{
        shopMapper.AuditShopReject(dto.getShopId());
        shopAuditLogMapper.add(new ShopAuditLog(dto.getShopId(),123L,new Date(),dto.getNote()));
        // 激活商店
        EmailUtil.send("您未通过审核请重写填写信息","http://localhost:8081/#/registerAgain?"+dto.getShopId());

    }

    @Override
    public void AuditShopPass(AuditShopDto dto) throws MessagingException {
        shopMapper.AuditShopPass(dto.getShopId());
        ShopAuditLog shopAuditLog = new ShopAuditLog();

        // 给日志依次添加ShopId，审核状态，日期，备注
        shopAuditLogMapper.add(new ShopAuditLog(dto.getShopId(),123L,new Date(),dto.getNote()));

        // 激活商店
        EmailUtil.send("恭喜你通过审核，点击链接激活","http://localhost:8080/shop/active/"+dto.getShopId());
    }

    @Override
    public void active(Long id) {
        shopMapper.active(id);
    }

    // 驳回重新填写
    @Override
    public void resetSettlement(Shop dto) {
        // 验证数据
//        this.verify(dto);

        // 修改数据
//        Shop hop = dto.getShop();
        dto.setState(0);
        shopMapper.update(dto);

        // 傻逼了，以为employee也要改
//        Employee insertAdmin = dto.getEmployee();
//       Employee emp = employeeMapper.findByShopId(dto.getId());
//        insertAdmin.setId(emp.getId());
//        employeeMapper.update(insertAdmin);
    }





    private void verify(ShopSettlementDto dto) {
        // 判断店铺名是否为空
        if (StringUtils.isBlank((dto.getName()))) {
            throw new BussinessException("店铺名称不能为空");
        }

        // 判断两次密码是否一致
        EmployeeDto admin = dto.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            System.out.println(admin.getPassword());
            System.out.println();
            System.out.println(admin.getComfirmPassword());
            throw new BussinessException("两次密码不一致");
        }

        // 店铺是否存在，根据店铺电话进行查询判断
        Shop shop = shopMapper.findByPhone(dto.getTel());
        if(null!=shop){
            throw new BussinessException("当前店铺已经存在");
        }

        // 当前入驻的管理员是否已经有管理的店铺
        Employee employee = employeeMapper.findByName(admin.getUsername());
        if(null!=employee && null!=employee.getShopId()){
            shop = shopMapper.findByShopId(employee.getShopId());
            // 该人是在店铺里但不知道是不是管理员，还要判断
            if(null!=shop && shop.getAdminId().equals(employee.getId())){
                throw new BussinessException("你已经是管理员了，一个管理员只能有一个店铺");
            }
        }

        // 百度ai审核
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            throw new BussinessException("您的店铺名字不合法");
        }
        if (!BaiduAuditUtils.ImgCensor(dto.getLogo())){
            throw new BussinessException("您的店铺logo不合法");
        }

    }

    private void save(ShopSettlementDto dto) {
        // 以上判断都未异常则保存数据到数据库
        // 保存店铺数据
        Shop insertShop = dto.getShop();
        shopMapper.add(insertShop);

        // 保存管理员数据
        Employee insertAdmin = dto.getEmployee();
        insertAdmin.setShopId(insertShop.getId());

        // 把emp中相同字段的值给logininfo
        Logininfo amind = new Logininfo();
        BeanUtils.copyProperties(insertAdmin,amind);

        // 添加数据到两张表
        logininfoMapper.add(amind);
        insertAdmin.setLogininfoId(amind.getId());
        employeeMapper.add(insertAdmin);


        // 虽然添加了数据，将其员工id,和管理员id关联说明这个人是店长
        insertShop.setAdminId(insertAdmin.getId());
        shopMapper.update(insertShop);
    }
}
